import html5lib
import os, geopandas as gpd
zelanda_estados=gpd.read_file(os.path.join("maps","New Zeland_Divisions","New Zeland_Divisions.shp"))
#Verificando si el tipo de datos que tenemos es GeoDataframe
type(zelanda_estados)
geopandas.geodataframe.GeoDataFrame
#Dimensiones
zelanda_estados.shape
(735, 10)
#Nombre de las columnas
zelanda_estados.columns
Index(['f_code', 'coc', 'nam', 'laa', 'pop', 'ypc', 'adm_code', 'salb', 'soc',
'geometry'],
dtype='object')
#Verificando estas columnas
zelanda_estados.head()
| f_code | coc | nam | laa | pop | ypc | adm_code | salb | soc | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | FA001 | NZL | North Auckland | UNK | -99999999 | 0 | UNK | UNK | NZL | POLYGON ((174.73207 -36.95977, 174.73417 -36.9... |
| 1 | FA001 | NZL | North Auckland | UNK | -99999999 | 0 | UNK | UNK | NZL | POLYGON ((174.46234 -36.96070, 174.46139 -36.9... |
| 2 | FA001 | NZL | North Auckland | UNK | -99999999 | 0 | UNK | UNK | NZL | POLYGON ((174.46615 -36.95461, 174.46595 -36.9... |
| 3 | FA001 | NZL | North Auckland | UNK | -99999999 | 0 | UNK | UNK | NZL | POLYGON ((174.67965 -36.87457, 174.67873 -36.8... |
| 4 | FA001 | NZL | North Auckland | UNK | -99999999 | 0 | UNK | UNK | NZL | POLYGON ((174.67457 -36.86594, 174.67463 -36.8... |
# Información
zelanda_estados.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 735 entries, 0 to 734 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 f_code 735 non-null object 1 coc 735 non-null object 2 nam 735 non-null object 3 laa 735 non-null object 4 pop 735 non-null int64 5 ypc 735 non-null int64 6 adm_code 735 non-null object 7 salb 735 non-null object 8 soc 735 non-null object 9 geometry 735 non-null geometry dtypes: geometry(1), int64(2), object(7) memory usage: 57.6+ KB
#Borrando todas las columnas excepto "nam" y "geometry" para mayor orden
byeColumns=['f_code', 'coc','laa', 'pop', 'ypc', 'adm_code', 'salb', 'soc']
#El resultado
zelanda_estados.drop(columns=byeColumns,inplace=True)
#Ahora
zelanda_estados
| nam | geometry | |
|---|---|---|
| 0 | North Auckland | POLYGON ((174.73207 -36.95977, 174.73417 -36.9... |
| 1 | North Auckland | POLYGON ((174.46234 -36.96070, 174.46139 -36.9... |
| 2 | North Auckland | POLYGON ((174.46615 -36.95461, 174.46595 -36.9... |
| 3 | North Auckland | POLYGON ((174.67965 -36.87457, 174.67873 -36.8... |
| 4 | North Auckland | POLYGON ((174.67457 -36.86594, 174.67463 -36.8... |
| ... | ... | ... |
| 730 | Southland | POLYGON ((167.59838 -44.74032, 167.59864 -44.7... |
| 731 | Southland | POLYGON ((167.91376 -44.68164, 167.91137 -44.6... |
| 732 | Southland | POLYGON ((167.91154 -44.67762, 167.91051 -44.6... |
| 733 | Southland | POLYGON ((167.63308 -44.64638, 167.63244 -44.6... |
| 734 | Southland | POLYGON ((169.26659 -46.62536, 169.26534 -46.6... |
735 rows × 2 columns
# Verificando si todos los valores existen
zelanda_estados[zelanda_estados.isna().any(axis=1)]
| nam | geometry |
|---|
#Visualizando las divisiones administrativas de Nueva Zelanda
zelanda_estados.plot()
<Axes: >
#Abriendo los shapefile de las vías férreas y los aeropuertos
railroad=gpd.read_file(os.path.join("maps","New Zeland_Railroads","New Zeland_Railroads.shp"))
aero=gpd.read_file(os.path.join("maps","New Zeland_Airports","New Zeland_Airports.shp"))
#Analizando la información dentro de "railroad"
railroad.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 152 entries, 0 to 151 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 f_code 152 non-null object 1 exs 152 non-null int64 2 fco 152 non-null int64 3 loc 152 non-null int64 4 soc 152 non-null object 5 geometry 152 non-null geometry dtypes: geometry(1), int64(3), object(2) memory usage: 7.3+ KB
#Analizando la información
aero.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 26 entries, 0 to 25 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 f_code 26 non-null object 1 iko 26 non-null object 2 ita 26 non-null object 3 nam 26 non-null object 4 use 26 non-null int64 5 zv3 26 non-null int64 6 soc 26 non-null object 7 geometry 26 non-null geometry dtypes: geometry(1), int64(2), object(5) memory usage: 1.8+ KB
railroad.head()
| f_code | exs | fco | loc | soc | geometry | |
|---|---|---|---|---|---|---|
| 0 | AN010 | 28 | 3 | 0 | NZL | LINESTRING (174.53098 -36.89878, 174.53213 -36... |
| 1 | AN010 | 28 | 3 | 0 | NZL | LINESTRING (175.18607 -37.54066, 175.18526 -37... |
| 2 | AN010 | 28 | 3 | 0 | NZL | LINESTRING (171.68327 -43.91570, 171.68401 -43... |
| 3 | AN010 | 28 | 3 | 0 | NZL | LINESTRING (176.89090 -39.49532, 176.89083 -39... |
| 4 | AN010 | 28 | 3 | 0 | NZL | LINESTRING (174.89642 -41.03294, 174.89695 -41... |
aero.head()
| f_code | iko | ita | nam | use | zv3 | soc | geometry | |
|---|---|---|---|---|---|---|---|---|
| 0 | GB005 | UNK | UNK | Whangarei Airport | 0 | -999 | NZL | POINT (174.36235 -35.77013) |
| 1 | GB005 | UNK | UNK | Bay of Islands Airport | 0 | -999 | NZL | POINT (173.91303 -35.25739) |
| 2 | GB005 | UNK | UNK | Invercargill Airport | 0 | -999 | NZL | POINT (168.31838 -46.41389) |
| 3 | GB005 | UNK | UNK | Dunedin Airport | 0 | -999 | NZL | POINT (170.19998 -45.92799) |
| 4 | GB005 | UNK | UNK | Alexandra Airport | 0 | -999 | NZL | POINT (169.36758 -45.20826) |
#Verificando el nombre de las columnas
aero.columns
Index(['f_code', 'iko', 'ita', 'nam', 'use', 'zv3', 'soc', 'geometry'], dtype='object')
#Borrando todas las columnas excepto "nam" y "geometry" para mayor rapidez en el manejo de datos
byeColumns=['f_code', 'iko', 'ita','use', 'zv3', 'soc']
#El resultado
aero.drop(columns=byeColumns,inplace=True)
#Ahora
aero
| nam | geometry | |
|---|---|---|
| 0 | Whangarei Airport | POINT (174.36235 -35.77013) |
| 1 | Bay of Islands Airport | POINT (173.91303 -35.25739) |
| 2 | Invercargill Airport | POINT (168.31838 -46.41389) |
| 3 | Dunedin Airport | POINT (170.19998 -45.92799) |
| 4 | Alexandra Airport | POINT (169.36758 -45.20826) |
| 5 | Queenstown Airport | POINT (168.74567 -45.01871) |
| 6 | Oamaru Airport | POINT (171.08512 -44.96801) |
| 7 | Timaru Airport | POINT (171.22831 -44.30225) |
| 8 | Aoraki/Mount Cook Airport | POINT (170.13650 -43.76507) |
| 9 | Christchurch International Airport | POINT (172.53358 -43.48853) |
| 10 | Hokitika Airport | POINT (170.98699 -42.71159) |
| 11 | Westport Airport | POINT (171.58307 -41.73827) |
| 12 | Nelson Airport | POINT (173.22662 -41.29456) |
| 13 | Marlborough Airport | POINT (173.86942 -41.51793) |
| 14 | Wellington International Airport | POINT (174.80615 -41.32602) |
| 15 | Wanganui Airport | POINT (175.02488 -39.96476) |
| 16 | Palmerston North Airport | POINT (175.61668 -40.32037) |
| 17 | Hawke's Bay Airport | POINT (176.86537 -39.46503) |
| 18 | New Plymouth Airport | POINT (174.17850 -39.00630) |
| 19 | Taupo Airport | POINT (176.08399 -38.74336) |
| 20 | Hamilton Airport | POINT (175.33153 -37.86397) |
| 21 | Rotorua Airport | POINT (176.31594 -38.10756) |
| 22 | Whakatane Airport | POINT (176.91698 -37.92132) |
| 23 | Tauranga Airport | POINT (176.19455 -37.67295) |
| 24 | Gisborne Airport | POINT (177.97495 -38.66034) |
| 25 | Auckland International Airport | POINT (174.78734 -37.00912) |
#Visualizando aeropuertos
aero.plot()
<Axes: >
#Verificando el nombre de las columnas
railroad.columns
Index(['f_code', 'exs', 'fco', 'loc', 'soc', 'geometry'], dtype='object')
#Borrando todas las columnas excepto "geometry"
byeColumns=['f_code', 'exs', 'fco', 'loc', 'soc']
#El resultado
railroad.drop(columns=byeColumns,inplace=True)
#Ahora
railroad
| geometry | |
|---|---|
| 0 | LINESTRING (174.53098 -36.89878, 174.53213 -36... |
| 1 | LINESTRING (175.18607 -37.54066, 175.18526 -37... |
| 2 | LINESTRING (171.68327 -43.91570, 171.68401 -43... |
| 3 | LINESTRING (176.89090 -39.49532, 176.89083 -39... |
| 4 | LINESTRING (174.89642 -41.03294, 174.89695 -41... |
| ... | ... |
| 147 | LINESTRING (171.56359 -42.83246, 171.56332 -42... |
| 148 | LINESTRING (168.33036 -46.59095, 168.32716 -46... |
| 149 | LINESTRING (168.79676 -46.28225, 168.79800 -46... |
| 150 | LINESTRING (176.16884 -37.66855, 176.16866 -37... |
| 151 | LINESTRING (172.69631 -43.56909, 172.69829 -43... |
152 rows × 1 columns
#Visualizando las vías férreas
railroad.plot()
<Axes: >
#Cambiando el estilo visual para los polígonos - división administrativa:
zelanda_estados.plot(facecolor="violet", edgecolor='black', linewidth=0.1)
<Axes: >
#Cambiando el estilo para las líneas - vías férreas:
railroad.plot(edgecolor='slategrey', linewidth=1, linestyle='dashed')
<Axes: >
#Cambiando el estilo visual para los puntos - aeropuertos:
aero.plot(marker='3', color='navy', markersize=3,alpha=0.8)
<Axes: >
zelanda_estados.crs
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...> Name: WGS 84 Axis Info [ellipsoidal]: - lon[east]: Longitude (degree) - lat[north]: Latitude (degree) Area of Use: - undefined Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
aero.crs
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...> Name: WGS 84 Axis Info [ellipsoidal]: - lon[east]: Longitude (degree) - lat[north]: Latitude (degree) Area of Use: - undefined Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
railroad.crs
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...> Name: WGS 84 Axis Info [ellipsoidal]: - lon[east]: Longitude (degree) - lat[north]: Latitude (degree) Area of Use: - undefined Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
#Como vemos, estos coinciden, aún así cambiaremos el crs para que coincidan como método de prevención
railroad=railroad.to_crs(zelanda_estados.crs)
aero=aero.to_crs(zelanda_estados.crs)
#Mapa completo del país partido en divisiones administrativas, vías férreas y aeropuertos
base = zelanda_estados.plot(facecolor="white", edgecolor='black', linewidth=0.1,figsize=(12,12))
aero.plot(marker='3', color='navy', markersize=4,alpha=0.8,
ax=base) # encima de...
railroad.plot(edgecolor='red', linewidth=0.4,
ax=base)# encima de...
<Axes: >
import folium
#Crear un objeto de mapa
m= folium.Map(zoom_start=10)
#Agregando cosas al mapa
folium.GeoJson(zelanda_estados.to_json(),name='Divisiones_administ',style_function=lambda feature: {'color':'red'}).add_to(m)
folium.GeoJson(railroad.to_json(),name='Railroads',style_function=lambda x: {'fillColor':'white','color':'green'}).add_to(m)
folium.GeoJson(aero.to_json(),name='Aeropuertos').add_to(m)
#El mapa
m